
//
//  ResetPassViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "ResetPassViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "RegisterNextViewController.h"
#import "CClass.h"
#import "CTextField.h"
#import "AppDelegate.h"
@interface ResetPassViewController()
{
    BOOL _hasSendCode;
    CTextField *_phoneText;
    CTextField *_passWordField;
    CTextField *_verField;
    NSInteger _eyeClick;
}
@property(nonatomic, strong)TPKeyboardAvoidingScrollView * scrollView;
@property(nonatomic, strong)UIButton *sendBtn;
@end
@implementation ResetPassViewController
- (void)initView
{
    [super initView];
    [self setTitle:@"重置密码"];
    [self layoutView];
    
}
- (void)layoutView
{
    _scrollView =[[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    _scrollView.backgroundColor =BackColor;
    [self.view addSubview:_scrollView];
    
    for (int i=0; i<3; i++) {
        UILabel*lab=[[UILabel alloc]init];
        lab.frame =CGRectMake(25, 5+(39+48)*i, WindowWidth-25, 39);
        lab.font= F(14);
        [_scrollView addSubview:lab];
        NSArray *ar =[[NSArray alloc]initWithObjects:@"手机号码",@"验证码",@"新密码",nil];
        lab.text =[ar objectAtIndex:i];
        lab.textColor=FontColor;
        UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lab.frame), WindowWidth, 48)];
        bgView.backgroundColor =[UIColor whiteColor];
        [_scrollView addSubview:bgView];
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
        line.backgroundColor =LineColor;
        [bgView addSubview:line];
        UILabel*line1=[[UILabel alloc]initWithFrame:CGRectMake(0, 47.5, WindowWidth, 0.5)];
        line1.backgroundColor =LineColor;
        [bgView addSubview:line1];
        
        if(i==0)
        {
            
            _phoneText =[[CTextField alloc]initWithFrame:CGRectMake(25, 0.5, WindowWidth-50, 47)];
            _phoneText.placeholder =@"请输入手机号码";
            [_phoneText setNullMsg:@"请输入11位手机号!"];
            _phoneText.font =F(12);
            [_phoneText setInputType:CTextFieldInputTypePhone];
            _phoneText.tag =100;
            [bgView addSubview:_phoneText];
            
        }else if(i==1)
        {
            _verField =[[CTextField alloc]init];
            _verField.frame =CGRectMake(25, 0.5, WindowWidth-130, 47);
            _verField.font =F(12);
            [_verField setMaxLength:6 LengthChangeBlock:nil];
            _verField.tag =101;
            _verField.inputType =CTextFieldInputTypeNum;
            _verField.placeholder = @"请输入验证码";
            [bgView addSubview:_verField];
            
            
            self.sendBtn =[UIButton buttonWithType:UIButtonTypeCustom];
            self.sendBtn.frame =CGRectMake(WindowWidth-106, 7.5, 86, 33);
            [self.sendBtn setBackgroundColor:SendColor];
            self.sendBtn.layer.masksToBounds=YES;
            self.sendBtn.layer.cornerRadius=4;
            self.sendBtn.userInteractionEnabled =YES;
            [self.sendBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            self.sendBtn.titleLabel.font =[UIFont systemFontOfSize:14];
            [self.sendBtn bk_addEventHandler:^(id sender) {
                if (!_phoneText.verfiy) {
                    return;
                }
                [CClass HttpRequestTask:YES withUrl:@"app_registryCheckCaptcha" withData:@{@"mobile":_phoneText.value,@"isCheckRegistry":@"0",@"customerType":@"02",@"sendIfRegistered":@"0"} notifyError:YES onSuccess:^(BaseModel *model) {
                    //成功的时候
                    
                    _hasSendCode = true;
                    
                    [self.sendBtn setEnabled:NO];
                    __block NSInteger t = 60;
                    NSTimeInterval period = 1.0; //设置时间间隔
                    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
                    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
                    dispatch_source_set_event_handler(_timer, ^{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.sendBtn setTitle:[NSString stringWithFormat:@"%zds", t] forState:UIControlStateNormal];
                            [self.sendBtn setBackgroundColor:[UIColor colorFromHexString:@"b3babd"]];
                            
                        });
                        if (t -- <= 0 ) {
                            dispatch_source_cancel(_timer);
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [self.sendBtn setEnabled:YES];
                                [self.sendBtn setTitle:@"重发验证码" forState:UIControlStateNormal];
                                [self.sendBtn setBackgroundColor:SendColor];
                            });
                        }
                    });
                    dispatch_resume(_timer);
                    
                } onError:^(BaseModel *model) {
                    
                    //出错时候
                }];
                
                
                
            } forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:self.sendBtn];
        }else
        {
            _passWordField =[[CTextField alloc]initWithFrame:CGRectMake(25, 0.5, WindowWidth-130, 47)];
            _passWordField.font =F(12);
            _passWordField.tag =102;
            _passWordField.secureTextEntry =YES;
            _passWordField.placeholder = @"请输入密码";
            [bgView addSubview:_passWordField];
            
            [_passWordField setInputType:CTextFieldInputTypePassword];
            [_passWordField setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *textField, NSRange range, NSString *str) {
                NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
                NSString *filtered = [[str componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [str isEqualToString:filtered];
            }];
            
            [_passWordField setMinLength:8 msg:@"请输入8-20位密码!"];
            [_passWordField setMaxLength:20 LengthChangeBlock:nil];
            [_passWordField bk_addEventHandler:^(id sender) {
                //移除表情
                NSString *newStr= [CClass disable_emoji:_passWordField.text];
                _passWordField.text=newStr;
                
                
            } forControlEvents:UIControlEventEditingChanged];
            UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame =CGRectMake(WindowWidth-86, 7.5, 86, 33);
            [button setImage:[UIImage imageNamed:@"Ico_register_noEye"] forState:UIControlStateNormal];
            [button bk_addEventHandler:^(id sender) {
                _eyeClick++;
                if (_eyeClick%2==0) {
                    _passWordField.secureTextEntry=YES;
                    [button setImage:[UIImage imageNamed:@"Ico_register_noEye"] forState:UIControlStateNormal];
                    
                }else{
                    _passWordField.secureTextEntry=NO;
                    [button setImage:[UIImage imageNamed:@"Ico_register_eye"] forState:UIControlStateNormal];
                }
            } forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:button];
        }
        
        
    }
    
    UIButton *loginBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame =CGRectMake(33, 305, WindowWidth-66, 35);
    [loginBtn setBackgroundColor:MainColor];
    loginBtn.layer.masksToBounds=YES;
    loginBtn.layer.cornerRadius=8;
    [loginBtn setTitle:@"确定" forState:UIControlStateNormal];
    loginBtn.titleLabel.font =F(15);
    [loginBtn bk_addEventHandler:^(id sender) {
        
        if (!_phoneText.verfiy) {
            return;
        }
        if (_hasSendCode == false) {
            [CClass showMessage:@"请先发送验证码!"];
            return ;
        }
        if (_verField.text.length==0) {
            [CClass showMessage:@"请输入验证码!"];
            return;
        }
        if (!_passWordField.verfiy) {
            return;
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:_phoneText.value forKey:@"mobile"];
        [dic setObject:_verField.text forKey:@"captcha"];
        [dic setObject:[CClass base64:_passWordField.text] forKey:@"password"];
        [dic setObject:@"02" forKey:@"customerType"];
        [self setIsLoading:YES];
        
        [CClass HttpRequestTask:YES withUrl:@"app_restPassword" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
//            15700168848
            //操作
            [CClass showMessage:@"密码重置成功，请重新登录" completion:^{
                [self backController];
            }];
            
        } onError:^(BaseModel *model) {
            
        }];
        
    } forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:loginBtn];
    
    
}
- (void) enter{
    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate enter];
}

@end
